﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Windows.Media;
using System.Collections.Specialized;

namespace TinyCinema.Bussiness.Converter
{
    public class BoolToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (bool)value ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class BoolToVisibilityConverter1 : MarkupExtension, IValueConverter
    {
        static BoolToVisibilityConverter1 converter;
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (bool)value ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (converter == null) converter = new BoolToVisibilityConverter1();
            return converter;
        }
        public BoolToVisibilityConverter1() { }
    }
    

    public class BoolToBrushConverter : MarkupExtension, IValueConverter
    {
        static BoolToBrushConverter converter;
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (bool)value ? new SolidColorBrush(Colors.MidnightBlue) : new SolidColorBrush(Colors.SkyBlue);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (converter == null) converter = new BoolToBrushConverter();
            return converter;
        }
        public BoolToBrushConverter() { }
    }
    public class BoolToBrushConverter1 : MarkupExtension, IValueConverter
    {
        static BoolToBrushConverter1 converter;
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (bool)value ? new SolidColorBrush(Colors.White) : new SolidColorBrush(Colors.Black);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (converter == null) converter = new BoolToBrushConverter1();
            return converter;
        }
        public BoolToBrushConverter1() { }
    }
    public class ListDeskToStringConverter : MarkupExtension, IValueConverter
    {
        static ListDeskToStringConverter converter;
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var lstDesk = value as ObservableCollection<Data.Desk>;
            if(lstDesk.Count>0)
            {
                string result = "";
                foreach (var Desk in lstDesk)
                    result += Desk.Range + Desk.Order + ", ";
                return result.Substring(0, result.Length - 2);
            }
            return "";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (converter == null) converter = new ListDeskToStringConverter();
            return converter;
        }
        public ListDeskToStringConverter() { }
    }
    public class DeskStatusToBrushConverter : MarkupExtension, IValueConverter
    {
        static DeskStatusToBrushConverter converter;
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            switch((Data.DeskStatus)value)
            {
                case Data.DeskStatus.Available: return Brushes.Transparent;
                case Data.DeskStatus.Ordered: return Brushes.Red;
                case Data.DeskStatus.Ordering: return Brushes.Green;
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (converter == null) converter = new DeskStatusToBrushConverter();
            return converter;
        }
        public DeskStatusToBrushConverter() { }
    }
    public class DeskTypeToBrushConverter : MarkupExtension, IValueConverter
    {
        static DeskTypeToBrushConverter converter;
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {            
            switch(int.Parse((value as Data.DeskType).ID.Substring(2,6))%8)
            {
                case 1: return Brushes.DarkTurquoise;
                case 2: return Brushes.DarkViolet;
                case 3: return Brushes.DeepPink;
                case 4: return Brushes.DimGray;
                case 5: return Brushes.Firebrick;
                case 6: return Brushes.Gold;
                case 7: return Brushes.GreenYellow;
            }
            return Brushes.Transparent;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (converter == null) converter = new DeskTypeToBrushConverter();
            return converter;
        }
        public DeskTypeToBrushConverter() { }
    }

    public class OppositeBoolToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (bool)value ? Visibility.Collapsed : Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class OppositeBoolToVisibilityConverter1 : MarkupExtension, IValueConverter
    {
        static OppositeBoolToVisibilityConverter1 converter;
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (bool)value ? Visibility.Collapsed : Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (converter == null) converter = new OppositeBoolToVisibilityConverter1();
            return converter;
        }
        public OppositeBoolToVisibilityConverter1() { }
    }
    public class RowToIndexConverter : MarkupExtension, IValueConverter
    {
        static RowToIndexConverter converter;

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            DataGridRow row = value as DataGridRow;
            if (row != null)
                return row.GetIndex() + 1;
            else
                return -1;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (converter == null) converter = new RowToIndexConverter();
            return converter;
        }

        public RowToIndexConverter()
        {
        }
    }
    public class ObjectToVisibilityConverter : MarkupExtension, IValueConverter
    {
        static ObjectToVisibilityConverter converter;

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return (value == null) ? Visibility.Collapsed : Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (converter == null) converter = new ObjectToVisibilityConverter();
            return converter;
        }

        public ObjectToVisibilityConverter()
        {
        }
    }
    public class ListActorToStringConverter : MarkupExtension, IValueConverter
    {
        static ListActorToStringConverter converter;

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var lst = value as ObservableCollection<Data.Actor>;
            string s = "";
            foreach (var Actor in lst)
                s += Actor.Name + ", ";
            if (s.Length>=2) return s.Substring(0, s.Length - 2);
            return s;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (converter == null) converter = new ListActorToStringConverter();
            return converter;
        }

        public ListActorToStringConverter()
        {
        }
    }
    public class ListGenreToStringConverter : MarkupExtension, IValueConverter
    {
        static ListGenreToStringConverter converter;

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var lst = value as ObservableCollection<Data.Genre>;
            string s = "";
            foreach (var Genre in lst)
                s += Genre.Name + ", ";
            if (s.Length >= 2) return s.Substring(0, s.Length - 2);
            return s;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (converter == null) converter = new ListGenreToStringConverter();
            return converter;
        }

        public ListGenreToStringConverter()
        {
        }
    }
    public class ListTicketToMovieConverter : MarkupExtension, IValueConverter
    {
        static ListTicketToMovieConverter converter;

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var lst = value as ObservableCollection<Data.Ticket>;
            string mov = "";
            foreach (var ticket in lst)
            {
                mov = ticket.Schedule.Movie.Name;
                break;
            }
            return mov;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (converter == null) converter = new ListTicketToMovieConverter();
            return converter;
        }

        public ListTicketToMovieConverter()
        {
        }
    }
    public class ListTicketToDeskConverter : MarkupExtension, IValueConverter
    {
        static ListTicketToDeskConverter converter;

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var lst = value as ObservableCollection<Data.Ticket>;
            string desks = "";
            foreach (var ticket in lst)
                desks += ticket.Desk.Range + ticket.Desk.Order + ", ";
            if (desks.Length > 0)
                return desks.Substring(0, desks.Length - 2);
            else return "";
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (converter == null) converter = new ListTicketToDeskConverter();
            return converter;
        }

        public ListTicketToDeskConverter()
        {
        }
    }
    public class ValueToPointConverter : MarkupExtension, IValueConverter
    {
        static ValueToPointConverter converter;

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var ticketorder = (decimal)value;
            return ticketorder / 1000;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (converter == null) converter = new ValueToPointConverter();
            return converter;
        }

        public ValueToPointConverter()
        {
        }
    }
    public class FocusingIndexToVisibilityConverter : MarkupExtension, IValueConverter
    {
        static FocusingIndexToVisibilityConverter converter;
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (int)value == 1 ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (converter == null) converter = new FocusingIndexToVisibilityConverter();
            return converter;
        }
        public FocusingIndexToVisibilityConverter() { }
    }
    public class ReportOpacityConverter : MarkupExtension, IValueConverter
    {
        static ReportOpacityConverter converter;
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            int param = int.Parse(parameter.ToString());
            System.Data.DataRow Row = value as System.Data.DataRow;
            var Rows = Row.Table.Rows;
            int preIndex = Rows.IndexOf(Row) - 1;
            if (Rows.IndexOf(Row)>0)
            {
                var x = Rows[preIndex][param];
                var y = Row[param];
                if (Rows[preIndex][param].ToString() == Row[param].ToString()) return 0;
            }
            return 1;
            
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (converter == null) converter = new ReportOpacityConverter();
            return converter;
        }
        public ReportOpacityConverter() { }
    }
}
